package day52.system.controller;

import day52.system.service.IPermissionService;
import day52.system.domain.Permission;
import day52.system.query.PermissionQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;


    /**
     * 保存和修改公用的
     * @param permission 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Permission permission) {
        if ( permission.getId() != null){
            permissionService.update(permission);
        }else{
            permissionService.add(permission);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        permissionService.delete(id);
        return AjaxResult.me();
    }

    //获取用户
    @GetMapping("/{id}")
    public Permission get(@PathVariable("id") Long id) {
        return permissionService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Permission> list() {
        return permissionService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Permission> json(@RequestBody PermissionQuery query) {
        return permissionService.pageQuery(query);
    }
}
